package workflow.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springside.core.dao.HibernateGenericDao;

import sysman.model.Backlog;

import workflow.model.ProcessDefinition;
import workflow.model.ProcessInstance;
import workflow.model.ProcessNode;
import workflow.model.ProcessTransition;
import workflow.model.ProcessVariable;
import workflow.util.XmlUtil;


public class ProcessDao extends HibernateGenericDao{
	
	/**
	 * 开始新的流程实例
	 * 
	 * @param processInstanceId
	 */
	private ProcessNode startProcess(Long processInstanceId) {
		ProcessNode task = null;
		String clName = ProcessNode.class.getName();
		try {
			String hql = "FROM " + clName + " WHERE proInsId = " + processInstanceId
					+ " AND nodeType = 'start-state'";
			List<ProcessNode> list = this.getHibernateTemplate().find(hql);
			ProcessInstance pi = this.findUniqueBy(ProcessInstance.class, "id", processInstanceId);
			ProcessDefinition pf = this.findUniqueBy(ProcessDefinition.class, "id", pi.getProDefId());
			boolean isApply = false;
			if("0".equals(pf.getType())){//工单派发流程
				isApply = false;
				this.execute4completeTask(list.get(0));
			} else {//审批流程
				isApply = true;
				this.execute(list.get(0), isApply);
			}
			
//			hql =  "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + processInstanceId + " AND currentNode = '1'";
			hql =  "FROM " + clName + " WHERE proInsId = " + processInstanceId + " AND visiable = '1' AND nodeType = 'task-node'";
			list = this.getHibernateTemplate().find(hql);
			task = list.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return task;
	}

	/**
	 * 根据传入的TASK执行下一节点
	 * 
	 * @param task
	 * @throws Exception
	 */
	private void execute(ProcessNode task, boolean isApply) throws RuntimeException {
		if ("end-state".equals(task.getNodeType())) {
			// 完成任务
			task.setStatus("2");
			this.getHibernateTemplate().save(task);

			// 完成流程
			List<ProcessInstance> list = this.getHibernateTemplate().find("FROM ProcessInstance WHERE id=" + task.getProInsId());
			ProcessInstance po = list.get(0);
			po.setStatus(ProcessInstance.STATUS_FINISH);
			this.getHibernateTemplate().save(po);
		} else if ("start-state".equals(task.getNodeType())) {
			String nodeFlag = task.getNodeFlag();
			String hql = "FROM ProcessTransition WHERE startTaskFlag = '"
					+ nodeFlag + "' AND proInsId = " + task.getProInsId() + " AND version=" + task.getVersion();
			List<ProcessTransition> list = this.getHibernateTemplate().find(hql);

			if (list == null || list.size() < 1) {
				throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
			} else {
				// 完成任务
				task.setStatus("2");
				this.getHibernateTemplate().save(task);
				// 执行下一节点
				ProcessTransition pt = list.get(0);
				nodeFlag = pt.getEndTaskFlag();
				hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
						+ " AND nodeFlag = '" + nodeFlag + "'"  + " AND version=" + task.getVersion();
				List<ProcessNode> tList = this.getHibernateTemplate().find(hql);
				pt.setStatus("0");
				this.getHibernateTemplate().save(pt);
				execute(tList.get(0), isApply);
			}
		} else if ("task-node".equals(task.getNodeType())) {			
			// 发送待办=======================================
			Long assignee = null;
			Long sendMan = null;// 发送人
			String acceMan = "";//接收人
			List<ProcessVariable> list = this.getHibernateTemplate().find(
					"FROM ProcessVariable WHERE proInsId=" + task.getProInsId()
							+ " AND varName='owner'"
							+ " AND version=" + task.getVersion());
			if(list != null && list.size() > 0){
				sendMan = Long.valueOf(list.get(0).getVarValue());
			}
			
			// 组装待办信息
			if (isApply) {
				// 流程实例
				ProcessInstance pi = this.findUniqueBy(ProcessInstance.class,
						"id", task.getProInsId());
				
				acceMan = task.getAssigneeExp();
				if (acceMan != null && !"".equals(acceMan.trim()) && acceMan.startsWith("#")) {
						acceMan = acceMan.replace("#", "").replace("{", "").replace("}", "").trim();
						String queryString = "FROM ProcessVariable WHERE varName = '"
								+ acceMan
								+ "' AND proInsId = "
								+ task.getProInsId()
								+ " AND version="
								+ task.getVersion();
						
						List<ProcessVariable> pvList = this.getHibernateTemplate().find(queryString);
						if (pvList == null || pvList.size() < 1)
							throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，流程中没有指定的变量。");
						
						ProcessVariable pv = pvList.get(0);
						String val = pv.getVarValue();
						acceMan = val;
				} else {
					acceMan = String.valueOf(task.getAssignee());
				}
				
				Backlog backlog = new Backlog();
				backlog.setAcceMan(acceMan);
				backlog.setSendDate(new Date());
				backlog.setSendMan(sendMan);
				backlog.setStatus("10");
				backlog.setTitle(pi.getName() + "_" + task.getName());
				backlog.setType("1");
				backlog.setUrl(task.getFormUrl() + "&taskId=" + task.getId());
				this.getHibernateTemplate().save(backlog);
			}
			
			// 分派任务=======================================
			task.setAssignee(Long.valueOf(acceMan));
			task.setVisiable("1");
			String hql = "UPDATE " + ProcessNode.class.getName() + " SET currentNode = '0' WHERE proInsId = " + task.getProInsId();
			this.getHibernateTemplate().bulkUpdate(hql);
			task.setCurrentNode("1");
			this.getHibernateTemplate().save(task);
			
		} else if ("decision".equals(task.getNodeType())) {
			// 完成任务
			task.setStatus("2");
			this.getHibernateTemplate().save(task);

			// 执行下一节点
			String expr = task.getExpression();
			if (expr == null || "".equals(expr) || "undefined".equals(expr)) {
				String nodeFlag = task.getNodeFlag();
				String hql = "FROM ProcessTransition WHERE startTaskFlag = '"
						+ nodeFlag + "' AND proInsId = " + task.getProInsId() + " AND version=" + task.getVersion();
				List<ProcessTransition> list = this.getHibernateTemplate()
						.find(hql);
				if (list == null || list.size() < 1) {
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
				} else {
					ProcessTransition pt = list.get(0);
					nodeFlag = pt.getEndTaskFlag();
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + nodeFlag + "'" + " AND version=" + task.getVersion();
					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);

					task.setStatus("2");
					this.getHibernateTemplate().save(task);

					execute(tList.get(0), isApply);
				}
			} else {
				// 解释表达式
				if (!expr.startsWith("#"))
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，没有以#开头。");
				else {
					expr = expr.replace("#", "");
					expr = expr.replace("{", "");
					expr = expr.replace("}", "");
				}
				String[] args = expr.split("\\?");
				if (args.length != 2)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，？号两则没有变量或值。");
				if(args[0].indexOf("==") == -1 && args[0].indexOf(">") == -1 && args[0].indexOf("<") == -1){
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，没有==、<、>号。");
				}
				String[] lSubArgs = args[0].split("==");
				if (lSubArgs.length != 2)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，==号两则没有变量或值。");
				String[] rSubArgs = args[1].replace("'", "").split(":");
				if (rSubArgs.length != 2)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，：号两则没有变量或值。");

				String var = lSubArgs[0];
				String varVal = lSubArgs[1];

				String hql = "FROM ProcessVariable WHERE varName = '" + var
						+ "' AND proInsId = " + task.getProInsId() + " AND version=" + task.getVersion();
				List<ProcessVariable> pvList = this.getHibernateTemplate()
						.find(hql);
				if (pvList == null || pvList.size() < 1)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，流程中没有指定的变量。");
				ProcessVariable pv = pvList.get(0);
				String val = pv.getVarValue();
				String transition = varVal.equals(val) == true ? rSubArgs[0]
						: rSubArgs[1];

				// 执行下一节点
				hql = "FROM ProcessTransition WHERE name = '" + transition
						+ "' AND proInsId = " + task.getProInsId()
						+ " AND startTaskFlag = '" + task.getNodeFlag() + "'" 
						+ " AND version=" + task.getVersion();
				List<ProcessTransition> ptList = this.getHibernateTemplate()
						.find(hql);
				if (ptList == null || ptList.size() < 1) {
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有<"
							+ transition + ">外转向");
				} else {
					ProcessTransition pt = ptList.get(0);
					String nodeFlag = pt.getEndTaskFlag();
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + nodeFlag + "'"  
							+ " AND version=" + task.getVersion();
					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);

					task.setStatus("2");
					this.getHibernateTemplate().save(task);

					execute(tList.get(0), isApply);
				}
			}
		} else if ("fork".equals(task.getNodeType())) {
			String hql = "FROM ProcessTransition WHERE proInsId = "
					+ task.getProInsId() + " AND startTaskFlag = '"
					+ task.getNodeFlag() + "'"  
					+ " AND version=" + task.getVersion();

			List<ProcessTransition> ptList = this.getHibernateTemplate().find(hql);

			if (ptList == null || ptList.size() < 1) {
				throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
			} else {
				for (ProcessTransition pt : ptList) {
					String nodeFlag = pt.getEndTaskFlag();
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + nodeFlag + "'" 
							+ " AND version=" + task.getVersion();

					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);

					task.setStatus("2");
					this.getHibernateTemplate().save(task);

					execute(tList.get(0), isApply);
				}
			}

		} else if ("join".equals(task.getNodeType())) {
			String hql = "FROM ProcessTransition WHERE proInsId = "
					+ task.getProInsId() + " AND endTaskFlag = '"
					+ task.getNodeFlag() + "'" 
					+ " AND version=" + task.getVersion();

			List<ProcessTransition> ptList = this.getHibernateTemplate().find(hql);

			if (ptList == null || ptList.size() < 1) {
				throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何内转向");
			} else {
				boolean isFinish = true;
				for (ProcessTransition pt : ptList) {
					if ("1".equals(pt.getStatus())) {
						isFinish = false;
						break;
					}
				}

				if (isFinish) {
					hql = "FROM ProcessTransition WHERE proInsId = "
							+ task.getProInsId() + " AND startTaskFlag = '"
							+ task.getNodeFlag() + "'" 
							+ " AND version=" + task.getVersion();

					ptList = this.getHibernateTemplate().find(hql);

					if (ptList == null || ptList.size() < 1) {
						throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
					} else {
						ProcessTransition pt = ptList.get(0);
						String nodeFlag = pt.getEndTaskFlag();
						hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = "
								+ task.getProInsId() + " AND nodeFlag = '"
								+ nodeFlag + "'" 
								+ " AND version=" + task.getVersion();

						List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

						pt.setStatus("0");
						this.getHibernateTemplate().save(pt);

						task.setStatus("2");
						this.getHibernateTemplate().save(task);

						execute(tList.get(0), isApply);
					}
				}
			}
		}
	}
	
	private void execute4completeTask(ProcessNode task) throws RuntimeException {		
		//更新流程进度
		if (task.getPercentPoint() != null
				&& !"".equals(task.getPercentPoint().trim())
				&& !"task-node".equals(task.getNodeType())) {
			String queryString = "UPDATE ProcessInstance set percent='"
					+ task.getPercentPoint() + "' WHERE id = "
					+ task.getProInsId();
			this.getHibernateTemplate().bulkUpdate(queryString);
		}
		
		// 更新标段进度，更新项目进度
		if (task.getPercentPoint() != null
				&& !"".equals(task.getPercentPoint().trim())
				&& !"task-node".equals(task.getNodeType())) {
			//projectProgressManager.calculateProgress(wb, task.getPercentPoint());
		}
		
		
		//流程流转		
		if ("end-state".equals(task.getNodeType())) {
			// 完成任务
			task.setStatus("2");
			this.getHibernateTemplate().save(task);

			// 完成流程
			List<ProcessInstance> list = this.getHibernateTemplate().find("FROM ProcessInstance WHERE id=" + task.getProInsId());
			ProcessInstance po = list.get(0);
			po.setStatus(ProcessInstance.STATUS_FINISH);
			this.getHibernateTemplate().save(po);
		} else if ("start-state".equals(task.getNodeType())) {
			String nodeFlag = task.getNodeFlag();
			String hql = "FROM ProcessTransition WHERE startTaskFlag = '"
					+ nodeFlag + "' AND proInsId = " + task.getProInsId() + " AND version=" + task.getVersion();
			List<ProcessTransition> list = this.getHibernateTemplate().find(hql);

			if (list == null || list.size() < 1) {
				throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
			} else {
				// 完成任务
				task.setStatus("2");
				this.getHibernateTemplate().save(task);
				// 执行下一节点
				ProcessTransition pt = list.get(0);
				nodeFlag = pt.getEndTaskFlag();
				hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
						+ " AND nodeFlag = '" + nodeFlag + "'"  + " AND version=" + task.getVersion();
				List<ProcessNode> tList = this.getHibernateTemplate().find(hql);
				pt.setStatus("0");
				this.getHibernateTemplate().save(pt);
				execute4completeTask(tList.get(0));
			}
		} else if ("task-node".equals(task.getNodeType())) {
			// 分派任务
			
			//==========
//			Task tempTask = this.reCompleteTask(task);
//			if(tempTask != null){
//				task = tempTask;
//			}		
			//==========	
			if (task.getAssignee() == null) {
				String hql = "";
				Long assignee = null;
				List<ProcessVariable> list = this.getHibernateTemplate().find("FROM ProcessVariable WHERE proInsId=" + task.getProInsId() 
						+ " AND version=" + task.getVersion());
				for (ProcessVariable po : list) {
					if ("owner".equals(po.getVarName())) {
						assignee = Long.valueOf(po.getVarValue());
						break;
					}
				}
				if (assignee != null)
					task.setAssignee(assignee);
			}
			
			task.setVisiable("1");
			this.getHibernateTemplate().save(task);
			
			
		} else if ("decision".equals(task.getNodeType())) {
			// 完成任务
			task.setStatus("2");
			this.getHibernateTemplate().save(task);

			// 执行下一节点
			String expr = task.getExpression();
			if (expr == null || "".equals(expr) || "undefined".equals(expr)) {
				String nodeFlag = task.getNodeFlag();
				String hql = "FROM ProcessTransition WHERE startTaskFlag = '"
						+ nodeFlag + "' AND proInsId = " + task.getProInsId() + " AND version=" + task.getVersion();
				List<ProcessTransition> list = this.getHibernateTemplate()
						.find(hql);
				if (list == null || list.size() < 1) {
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
				} else {
					ProcessTransition pt = list.get(0);
					nodeFlag = pt.getEndTaskFlag();
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + nodeFlag + "'" + " AND version=" + task.getVersion();
					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);

					task.setStatus("2");
					this.getHibernateTemplate().save(task);

					execute4completeTask(tList.get(0));
				}
			} else {
				// 解释表达式
				if (!expr.startsWith("#"))
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，没有以#开头。");
				else {
					expr = expr.replace("#", "");
					expr = expr.replace("{", "");
					expr = expr.replace("}", "");
				}
				String[] args = expr.split("\\?");
				if (args.length != 2)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，？号两则没有变量或值。");
				if(args[0].indexOf("==") == -1 && args[0].indexOf(">") == -1 && args[0].indexOf("<") == -1){
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，没有==、<、>号。");
				}
				String[] lSubArgs = args[0].split("==");
				if (lSubArgs.length != 2)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，==号两则没有变量或值。");
				String[] rSubArgs = args[1].replace("'", "").split(":");
				if (rSubArgs.length != 2)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，：号两则没有变量或值。");

				String var = lSubArgs[0];
				String varVal = lSubArgs[1];

				String hql = "FROM ProcessVariable WHERE varName = '" + var
						+ "' AND proInsId = " + task.getProInsId() + " AND version=" + task.getVersion();
				List<ProcessVariable> pvList = this.getHibernateTemplate()
						.find(hql);
				if (pvList == null || pvList.size() < 1)
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">表达式错误，流程中没有指定的变量。");
				ProcessVariable pv = pvList.get(0);
				String val = pv.getVarValue();
				String transition = varVal.equals(val) == true ? rSubArgs[0]
						: rSubArgs[1];

				// 执行下一节点
				hql = "FROM ProcessTransition WHERE name = '" + transition
						+ "' AND proInsId = " + task.getProInsId()
						+ " AND startTaskFlag = '" + task.getNodeFlag() + "'" 
						+ " AND version=" + task.getVersion();
				List<ProcessTransition> ptList = this.getHibernateTemplate()
						.find(hql);
				if (ptList == null || ptList.size() < 1) {
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有<"
							+ transition + ">外转向");
				} else {
					ProcessTransition pt = ptList.get(0);
					String nodeFlag = pt.getEndTaskFlag();
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + nodeFlag + "'"  
							+ " AND version=" + task.getVersion();
					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);

					task.setStatus("2");
					this.getHibernateTemplate().save(task);

					execute4completeTask(tList.get(0));
				}
			}
		} else if ("fork".equals(task.getNodeType())) {
			String hql = "FROM ProcessTransition WHERE proInsId = "
					+ task.getProInsId() + " AND startTaskFlag = '"
					+ task.getNodeFlag() + "'"  
					+ " AND version=" + task.getVersion();

			List<ProcessTransition> ptList = this.getHibernateTemplate().find(hql);

			if (ptList == null || ptList.size() < 1) {
				throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
			} else {
				for (ProcessTransition pt : ptList) {
					String nodeFlag = pt.getEndTaskFlag();
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + nodeFlag + "'" 
							+ " AND version=" + task.getVersion();

					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);

					task.setStatus("2");
					this.getHibernateTemplate().save(task);

					this.execute4completeTask(tList.get(0));
				}
			}

		} else if ("join".equals(task.getNodeType())) {
			String hql = "FROM ProcessTransition WHERE proInsId = "
					+ task.getProInsId() + " AND endTaskFlag = '"
					+ task.getNodeFlag() + "'" 
					+ " AND version=" + task.getVersion();

			List<ProcessTransition> ptList = this.getHibernateTemplate().find(hql);

			if (ptList == null || ptList.size() < 1) {
				throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何内转向");
			} else {
				boolean isFinish = true;
				for (ProcessTransition pt : ptList) {
					if ("1".equals(pt.getStatus())) {
						isFinish = false;
						break;
					}
				}

				if (isFinish) {
					hql = "FROM ProcessTransition WHERE proInsId = "
							+ task.getProInsId() + " AND startTaskFlag = '"
							+ task.getNodeFlag() + "'" 
							+ " AND version=" + task.getVersion();

					ptList = this.getHibernateTemplate().find(hql);

					if (ptList == null || ptList.size() < 1) {
						throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
					} else {
						ProcessTransition pt = ptList.get(0);
						String nodeFlag = pt.getEndTaskFlag();
						hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = "
								+ task.getProInsId() + " AND nodeFlag = '"
								+ nodeFlag + "'" 
								+ " AND version=" + task.getVersion();

						List<ProcessNode> tList = this.getHibernateTemplate().find(hql);

						pt.setStatus("0");
						this.getHibernateTemplate().save(pt);

						task.setStatus("2");
						this.getHibernateTemplate().save(task);

						execute4completeTask(tList.get(0));
					}
				}
			}
		}
	}

	/**
	 *开始新的流程实例
	 * 
	 * @param po           流程实例
	 * @param variableMap  流程变量表
	 * @return
	 */
	public ProcessNode startProcessInstanceById(ProcessInstance po, HashMap<String, String> variableMap) throws RuntimeException{
		ProcessNode task = null;
		try{
			XmlUtil xmlUtil = new XmlUtil();
			
			this.getHibernateTemplate().save(po);
			
			List taskList = xmlUtil.buildTask(po.getProcessXml(), po.getId(), variableMap, 1L);
			
			this.getHibernateTemplate().saveOrUpdateAll(taskList);
			
			task = startProcess(po.getId());
			
		} catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		
		return task;
	}
	
	/**
	 * 完成关闭一个任务，并且触发流程按指定的转向流向下一节点。
	 * 
	 * @param task    需要关闭的任务
	 * @param varList 变量表
	 * @param outcome 指定的外转向
	 */
	public void completeTask(ProcessNode task, List<ProcessVariable> varList, String outcome) throws RuntimeException{
		try {
			
			//==========================================================================
			//更新流程进度
			if (task.getPercentPoint() != null
					&& !"".equals(task.getPercentPoint().trim())) {
				String queryString = "UPDATE ProcessInstance set percent='"
						+ task.getPercentPoint() + "' WHERE id = "
						+ task.getProInsId();
				this.getHibernateTemplate().bulkUpdate(queryString);
			}
			// 更新标段进度，更新项目进度
			
			if (task.getPercentPoint() != null
					&& !"".equals(task.getPercentPoint().trim())) {
				//projectProgressManager.calculateProgress(wb, task.getPercentPoint());
			}
			
			//流程流转
			String hql = "FROM ProcessVariable WHERE proInsId = " + task.getProInsId();
			List<ProcessVariable> pvList = this.getHibernateTemplate().find(hql);
			
			String varName = null;
			boolean isSameVar = false;
			for(ProcessVariable pv : varList){
				isSameVar = false;
				varName = pv.getVarName();
				for(ProcessVariable po : pvList){
					if(varName.equals(po.getVarName())){
						po.setVarValue(pv.getVarValue());
						isSameVar = true;
						break;
					}
				}
				
				if(!isSameVar){
					pvList.add(pv);
				}
			}
			
			this.getHibernateTemplate().save(task);			
			this.getHibernateTemplate().saveOrUpdateAll(pvList);
			
			
			if (outcome != null) {
				hql = "FROM ProcessTransition WHERE proInsId = "
						+ task.getProInsId() + " AND startTaskFlag = '"
						+ task.getNodeFlag() + "' AND name = '" + outcome + "'" 
						+ " AND version=" + task.getVersion();
				List<ProcessTransition> list = this.getHibernateTemplate().find(hql);

				if (list == null || list.size() < 1) {
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有<" + outcome + ">外转向");
				} else {
					ProcessTransition pt = list.get(0);
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + pt.getEndTaskFlag() + "'" 
							+ " AND version=" + task.getVersion();
					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);
					
					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);
					
					ProcessNode ta = tList.get(0);
					this.execute4completeTask(ta);
				}
			} else {
				hql = "FROM ProcessTransition WHERE proInsId = "
						+ task.getProInsId() + " AND startTaskFlag = '"
						+ task.getNodeFlag() + "'" 
						+ " AND version=" + task.getVersion();
				List<ProcessTransition> list = this.getHibernateTemplate().find(hql);

				if (list == null || list.size() < 1) {
					throw new RuntimeException("流程错误：节点<" + task.getName() + ">没有任何外转向");
				} else {
					ProcessTransition pt = list.get(0);
					String endTaskFlag = pt.getEndTaskFlag();
					hql = "FROM " + ProcessNode.class.getName() + " WHERE proInsId = " + task.getProInsId()
							+ " AND nodeFlag = '" + pt.getEndTaskFlag() + "'" 
							+ " AND version=" + task.getVersion();
					List<ProcessNode> tList = this.getHibernateTemplate().find(hql);
					
					pt.setStatus("0");
					this.getHibernateTemplate().save(pt);
					
					ProcessNode ta = tList.get(0);
					this.execute4completeTask(ta);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
	public void saveOrUpdateAll(List entities) throws RuntimeException{
		this.getHibernateTemplate().saveOrUpdateAll(entities);
	}




}
